# -*- coding: utf-8 -*-
import numpy as np
import math
import csv

def preprocess(src, dest):
	f = csv.reader(open(src, 'r'))
	out = open(dest, 'a', newline='')
	w = csv.writer(out, dialect='excel')

	flag = 0
	count = 0		# 一秒内的第几条记录
	gnss_count = 0
	mag1 = []
	mag2 = []
	mag3 = []
	light_list = []
	light_count = 0
	light_dim = 9

	for rec in f:
		if flag == 0:
			flag = 1
			continue

		count += 1

		# 处理地磁数据
		if count % 15 == 0:
			mag = rec[7].split(';')
			mag1.append(float(mag[0]))
			mag2.append(float(mag[1]))
			mag3.append(float(mag[2]))

		# 处理光强数据
		light = float(rec[10])
		#if light == 0:
			#light = 0.5
		if light_count == 0 or light_list[light_count - 1] != light:
			light_list.append(light)
			light_count += 1

		# 处理gnssStatus，生成整理的数据
		if len(rec[12]) > 1 or rec[12] == '0':
			gnss_count += 1
			if gnss_count <= 5:
				continue

			cnr = []
			altitude = []

			if len(rec[12]) > 1:
				each_sate = rec[12].split(' ')
				sate_num = float(each_sate[0])
				del each_sate[0]
				last3 = []
				for item in each_sate:
					last3 = item.split(';')
					if last3[-1] != '0.0' and last3[-2] != '0.0' and last3[-3] != '0.0':
						altitude.append(float(last3[-2]))
						cnr.append(float(last3[-3]))

			if len(cnr) == 0:
				sate_num = 0
				cnr = [0, 0]
				altitude = [0, 0]

			if len(mag1) >= 4:
				row = []
				row.append(float(rec[4]))	# 标签

				for i in range(-4, 0):
					row.append(mag1[i])		# 地磁第1维
				for i in range(-4, 0):
					row.append(mag2[i])		# 地磁第2维
				for i in range(-4, 0):	
					row.append(mag3[i])		# 地磁第3维

				row.append(float(rec[1]) ** 2)	# hour

				# 将光强填充到指定维度
				light_added = 0
				each_dim = light_dim // len(light_list)
				for l in light_list:
					l_sqrt = l ** 0.5
					for i in range(each_dim):
						row.append(l_sqrt)
						light_added += 1
				last_sqrt = light_list[-1] ** 0.5
				while light_added < light_dim:
					row.append(last_sqrt)
					light_added += 1

				row.append(sate_num)
				row.append(np.mean(cnr))
				row.append(np.std(cnr))
				row.append(np.mean(altitude))
				row.append(np.std(altitude))
				w.writerow(row)

			count = 0
			light_list = []
			light_count = 0
			mag1 = []
			mag2 = []
			mag3 = []

	return (gnss_count - 5)


if __name__ == "__main__":
	f = open('./list_m8.txt') # mate8所有csv的路径
	dest = './data_m8.csv'
	total = 0
	for i in range(1347):
		src = f.readline()
		total += preprocess(src[:-1], dest)
	print(total)

	f = open('./list_m9.txt') # mate9所有csv的路径
	dest = './data_m9.csv'
	total = 0
	for i in range(443):
		src = f.readline()
		total += preprocess(src[:-1], dest)
	print(total)

	f = open('./list_hv.txt') # honor和vivo所有csv的路径
	dest = './data_hv.csv'
	total = 0
	for i in range(828):
		src = f.readline()
		total += preprocess(src[:-1], dest)
	print(total)
